package com.actions.modelo;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.mapping.Modelo;
import com.mapping.Marca;
import com.mapping.TipoDisp;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author PARDmx
 */
@Namespace(value = "/")
@Action(value = "gestionModificarModeloPreP", results = {
    @Result(name = "success", location = "/GestionModelos/gestionModificarModelo.jsp")})
public class ModificarModeloPre extends ActionSupport{
    private Modelo modelo;
    private Marca marca;
    private Marca marcas[];
    private TipoDisp disp;
    private TipoDisp disps[];
    private String idModelo;

    @Override
    public String execute() throws Exception {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin(); 
        modelo = (Modelo)em.getReference(Modelo.class, Integer.parseInt(idModelo));
        marca = modelo.getIdMarca();
        TypedQuery  e=em.createQuery("SELECT m FROM Marca m", Marca.class);    
        List<Marca> resultList = e.getResultList();
        marcas = new Marca[resultList.size()];
                
        for(int i=0; i <resultList.size(); i++){                                             
            marcas[i] = resultList.get(i);           
        }         
        
        disp = modelo.getIdTipo();
        TypedQuery  e1=em.createQuery("SELECT t FROM TipoDisp t", TipoDisp.class);    
        List<TipoDisp> resultList1 = e1.getResultList();
        disps = new TipoDisp[resultList1.size()];
                
        for(int i=0; i <resultList1.size(); i++){                                             
            disps[i] = resultList1.get(i);           
        }                 
        
        em.flush();
        tx.commit();
        em.close();                 
        return SUCCESS;        
        
    }

    public Modelo getModelo() {
        return modelo;
    }

    public Marca getMarca() {
        return marca;
    }

    public Marca[] getMarcas() {
        return marcas;
    }

    public TipoDisp getDisp() {
        return disp;
    }

    public TipoDisp[] getDisps() {
        return disps;
    }

    public void setIdModelo(String idModelo) {
        this.idModelo = idModelo;
    }
    
    
}
